<?php

namespace frontend\controllers;

use Yii;
use common\models\Doc;
use common\models\DocSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Word;
use common\models\Creditdetail;
use common\models\User;

use yii\filters\AccessControl;
/**
 * DocController implements the CRUD actions for Doc model.
 */
class DocController extends Controller
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        		'access' =>[
        				'class' => AccessControl::className(),
        				'rules' =>
        				[
        						[
        								'actions' => ['index','category','advancedindex','view'],
        								'allow' => true,
        								'roles' => ['?'],
        								],
        								[
        										'actions' => ['index', 'detail','category','advancedindex','view','update','create'],
        										'allow' => true,
        										'roles' => ['@'],
        						],
        						],
        						],
        ];
    }

    /**
     * Lists all Doc models.
     * @return mixed
     */
    public function actionIndex()
    {
		$words=Word::findWordWeights();
		$recentCreditdetails=Creditdetail::findRecentCreditdetails();
		$recentDocs=Doc::findRecentDocs();
		$hotDocs=Doc::findHotDocs();
        $searchModel = new DocSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
			'words'=>$words,
			'recentCreditdetails'=>$recentCreditdetails,
			'recentDocs'=>$recentDocs,
			'hotDocs'=>$hotDocs,
        ]);
    }
    /**
     * Lists all Doc models.
     * @return mixed
     */
    public function actionAdvancedindex()
    {
        $searchModel = new DocSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('advancedindex', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Doc model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
	     $model = Doc::findOne($id);
	     $viewnum=$model->viewnum;
		 $viewnum++;
		 $model->viewnum=$viewnum;
		 $model->save();
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    public function actionAdvancedview($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    /**
     * Creates a new Doc model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Doc();
		$model->time=time();
		$model->lastedit=time(); 
		$model->status = 1;
		// $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['detail', 'id' => $model->did]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Doc model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($did)
    {
		// $id=$_GET['did'];

        $modele = $this->findModel($did);
		$userMe = User::findOne(Yii::$app->user->id);
		$maxid = Doc::find()->max('did');
        $model= new Doc();
		// $docinfo= $docmodel->findOne($id);
		$model->did=$maxid+1;
		// $model->insert();
	
		// $did=$maxid+1;
		$model->cid=$modele->cid;
		$model->title=$modele->title;
		$model->keywords=$modele->keywords;
		$model->authorname=$modele->authorname;
		$model->summary=$modele->summary;
		$model->content=$modele->content;
		$model->locked=$modele->locked;
		// $model->lasteditorname= $userMe->name;
		$model->time=$modele->time;
		$model->lastedit=time(); 
		$model->status = 1;
		
		// $list = Doc::find()->all();
		// print_r($list);
			// exit;
        if ($model->load(Yii::$app->request->post())) {

            return $this->redirect(['detail', 'did'  => $model->did]);
        } else {
            return $this->render('update', [
                'model' => $model,
				// 'edited'=>$this->edited, 
            ]);
        }


			
    }

    /**
     * Deletes an existing Doc model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Doc model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Doc the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Doc::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

	    public function actionDetail($did)
    {
		$words=Word::findWordWeights();
		$recentCreditdetails=Creditdetail::findRecentCreditdetails();
		$recentDocs=Doc::findRecentDocs();
		$hotDocs=Doc::findHotDocs();
        $model = Doc::findOne($did);
        if ($model == null) {
            throw new NotFoundHttpException;
        }
        $viewnum=$model->viewnum;
		$viewnum++;
		$model->viewnum=$viewnum;
		 $model->save();
        return $this->render('detail', [
		    'model'=>$model,
			'words'=>$words,
			'recentCreditdetails'=>$recentCreditdetails,
			'recentDocs'=>$recentDocs,
			'hotDocs'=>$hotDocs,

        ]);
    	
    }
	public function actionCategory()
    {
		$words=Word::findWordWeights();
		$recentCreditdetails=Creditdetail::findRecentCreditdetails();
		// $recentDocs=Doc::findRecentDocs();
		// $hotDocs=Doc::findHotDocs();
        // $model = Doc::findOne();
		 $searchModel = new DocSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
 

        return $this->render('category', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
			'words'=>$words,
			'recentCreditdetails'=>$recentCreditdetails,
			// 'recentDocs'=>$recentDocs,
			// 'hotDocs'=>$hotDocs,
        ]);
    	
    }
}
